<template>
  <div class="register-form-wrapper">
    <div class="register-form-title">注册</div>
    <div class="register-form-error-msg">{{ errorMessage }}</div>
    <a-form
      ref="registerForm"
      :model="formData"
      class="register-form"
      layout="vertical"
      @submit="handleSubmit"
    >
      <a-form-item
        field="userAccount"
        :rules="[
          { required: true, message: '请您输入用户账号' },
          { minLength: 4, message: '账号不能小于4位' },
        ]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input v-model="formData.userAccount" placeholder="用户名：admin">
          <template #prefix>
            <icon-user />
          </template>
        </a-input>
      </a-form-item>
      <a-form-item
        field="userPassword"
        :rules="[
          { required: true, message: '密码不能为空' },
          { minLength: 8, message: '密码不能小于8位' },
        ]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input-password
          v-model="formData.userPassword"
          placeholder="密码"
          allow-clear
        >
          <template #prefix>
            <icon-lock />
          </template>
        </a-input-password>
      </a-form-item>
      <a-form-item
        field="checkPassword"
        :rules="[
          { required: true, message: '确认密码不能为空' },
          { minLength: 8, message: '确认密码不能小于8位' },
          {
            validator: async (value: string,cb) => {
              if (value !== formData.userPassword) {
                cb('两次密码不一致')
              }
             }
          }
        ]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input-password
          v-model="formData.checkPassword"
          placeholder="确认密码"
          allow-clear
        >
          <template #prefix>
            <icon-lock />
          </template>
        </a-input-password>
      </a-form-item>
      <a-space :size="16" direction="vertical">
        <a-button type="primary" html-type="submit" long :loading="loading">
          注册
        </a-button>
        <a-button type="text" long class="register-form-register-btn">
          返回
        </a-button>
      </a-space>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { useRouter } from 'vue-router';
  import { useUserStore } from '@/store';
  import useLoading from '@/hooks/loading';
  import { UserControllerService } from '@/api/generated';
  import { Message } from '@arco-design/web-vue';

  const router = useRouter();
  const errorMessage = ref('');
  const { loading, setLoading } = useLoading();
  const userStore = useUserStore();

  const formData = reactive({
    userAccount: 'admin',
    userPassword: '12345678',
    checkPassword: '12345678',
  });

  const handleSubmit = async () => {
    setLoading(true);
    try {
      const res = await UserControllerService.userRegisterUsingPost(formData);
      if (res) {
        Message.success('注册成功');
      }
    } finally {
      setLoading(false);
    }
  };
</script>

<style lang="less" scoped>
  .register-form {
    &-wrapper {
      width: 320px;
    }

    &-title {
      color: var(--color-text-1);
      font-weight: bold;
      font-size: 30px;
      line-height: 32px;
    }

    &-sub-title {
      color: var(--color-text-3);
      font-size: 16px;
      line-height: 24px;
    }

    &-error-msg {
      height: 32px;
      color: rgb(var(--red-6));
      line-height: 32px;
    }

    &-password-actions {
      display: flex;
      justify-content: space-between;
    }

    &-register-btn {
      color: var(--color-text-3) !important;
    }
  }
</style>
